﻿using System;
using System.Web.UI.WebControls;

namespace ECommerce.Administration.Controls
{
	public partial class PaymentmethodList : AdminBaseUsercontrol
	{
		private Service.IPaymentMethodService _PaymentMethodService;

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindData();
			}
		}

		protected void GridPaymentMethod_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.PaymentMethodList.NameHeader");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.PaymentMethodList.VisibleNameHeader");
				e.Row.Cells[3].Text = GetLocalResourceString("Admin.PaymentMethodList.ActiveHeader");
				e.Row.Cells[4].Text = GetLocalResourceString("Admin.PaymentMethodList.DisplayOrderHeader");
			}

			if(e.Row.DataItem != null)
			{
				var item = e.Row.DataItem as Data.Entity.PaymentMethod;

				HyperLink HyperLinkPaymentMethod = e.Row.FindControl("HyperLinkPaymentMethod") as HyperLink;

				HyperLinkPaymentMethod.NavigateUrl = "~/Administration/EditPaymentMethod.aspx?paymentmethodid=" + item.PaymentMethodID.ToString();
				HyperLinkPaymentMethod.Text = item.Name;
			}
		}

		private void BindData()
		{
			if (_PaymentMethodService == null)
			{
				_PaymentMethodService = new Service.PaymentMethodService();
			}

			var data = _PaymentMethodService.GetAllPaymentMethod();

			GridPaymentMethod.DataSource = data;
			GridPaymentMethod.DataBind();
		}

		public void DeleteSelectedPaymentMethod()
		{
			if (_PaymentMethodService == null)
			{
				_PaymentMethodService = new Service.PaymentMethodService();
			}

			foreach(System.Web.UI.WebControls.GridViewRow rowItem in GridPaymentMethod.Rows)
			{
				var chBox = rowItem.FindControl("cbPaymentMethod") as CheckBox;
				var hfPaymentMethodID = rowItem.FindControl("hfPaymentMethodId") as HiddenField;

				bool isChecked = chBox.Checked;
				int paymentMethodID = int.Parse(hfPaymentMethodID.Value);

				if(isChecked)
				{
					_PaymentMethodService.DeletePaymentMethod(paymentMethodID);
				}
			}

			BindData();
		}
	}
}